﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using RM.DBUtility;
using RM.Common;
namespace RM.DAL
{
    public partial class menu_list
    {
        public menu_list()
        { }

        #region 基本方法
        /// <summary>
        /// 根据ID判断是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from X_Menu ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int)};
            parameters[0].Value = id;
            return DbHelperSQL.Exists(strSql.ToString());
        }
        /// <summary>
        /// 根据标题判断是否存在
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public bool Exists(string title)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from X_Menu");
            strSql.Append(" where title=@title");
            SqlParameter[] parameters ={
                                          new SqlParameter("@title",SqlDbType.VarChar,200)};
            parameters[0].Value = title;
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 通过ID获得一个对象实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Model.menu_list GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,modulename,title,navigateurl,imageurl,sort_id,parentid,remark from X_Menu");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int)};
            parameters[0].Value = id;
            Model.menu_list model = new Model.menu_list();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.ModuleName = ds.Tables[0].Rows[0]["modulename"].ToString();
                model.Title = ds.Tables[0].Rows[0]["title"].ToString();
                model.NavigateUrl = ds.Tables[0].Rows[0]["navigateurl"].ToString();
                model.ImageUrl = ds.Tables[0].Rows[0]["imageurl"].ToString();
                if (ds.Tables[0].Rows[0]["sortindex"].ToString() != "")
                {
                    model.SortIndex = int.Parse(ds.Tables[0].Rows[0]["sortindex"].ToString());
                }
                if (ds.Tables[0].Rows[0]["parentmenuid"].ToString() != "")
                {
                    model.ParentMenuId = int.Parse(ds.Tables[0].Rows[0]["parentmenuid"].ToString());
                }
                model.Remark = ds.Tables[0].Rows[0]["remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }            
        }
        /// <summary>
        /// 通过导航标题获得一个对象实例
        /// </summary>
        /// <param name="nav_title"></param>
        /// <returns></returns>
        public Model.menu_list GetModel(string nav_title)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,modulename,title,navigateurl,imageurl,sortindex,parentmenuid,remark");
            strSql.Append(" from X_Menu");
            strSql.Append(" where title=@title");
            SqlParameter[] parameters ={
                                          new SqlParameter("@title",SqlDbType.VarChar,200)};
            parameters[0].Value = nav_title;
            Model.menu_list model = new Model.menu_list();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.ModuleName = ds.Tables[0].Rows[0]["modulename"].ToString();
                model.Title = ds.Tables[0].Rows[0]["title"].ToString();
                model.NavigateUrl = ds.Tables[0].Rows[0]["navigateurl"].ToString();
                model.ImageUrl = ds.Tables[0].Rows[0]["imageurl"].ToString();
                if (ds.Tables[0].Rows[0]["sortindex"].ToString() != "")
                {
                    model.SortIndex = int.Parse(ds.Tables[0].Rows[0]["sortindex"].ToString());
                }
                if (ds.Tables[0].Rows[0]["parentmenuid"].ToString() != "")
                {
                    model.ParentMenuId = int.Parse(ds.Tables[0].Rows[0]["parentmenuid"].ToString());
                }
                model.Remark = ds.Tables[0].Rows[0]["remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 取得父ID下的列表
        /// </summary>
        /// <param name="parentmenuid">父ID</param>
        /// <returns></returns>
        public DataTable GetChildList(int parentMenuId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,modulename,title,navigateurl,imageurl,sortindex,parentmenuid,remark");
            strSql.Append(" from X_Menu");
            strSql.Append(" where parentmenuid=" + parentMenuId + " order by sortindex asc,id asc");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            return ds.Tables[0];
        }
        /// <summary>
        /// 取得所有列表（没有排序好，只有数据）
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,modulename,title,navigateurl,imageurl,sortindex,parentmenuid,remark");
            strSql.Append(" from X_Menu");
            strSql.Append(" order by sortindex asc,id asc");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            return ds.Tables[0];
        }

        public DataTable GetList(int parentMenuId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,modulename,title,navigateurl,imageurl,sortindex,parentmenuid,remark");
            strSql.Append(" from X_Menu");
            strSql.Append(" order by sortindex asc,id asc");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DataTable oldData = ds.Tables[0] as DataTable;
            if (oldData == null)
            {
                return null;
            }
            //复制结构
            DataTable newData = oldData.Clone();
            //调用迭代组合成DataTable
            GetChilds(oldData, newData, parentMenuId);
            return newData;
        }
        #endregion
        
        #region 私有方法
        /// <summary>
        /// 从内存中取得所有下级类别列表(自身迭代)
        /// </summary>
        
        private void GetChilds(DataTable oldData, DataTable newData, int parentMenuId)
        {
            DataRow[] dr = oldData.Select("parentmenuid=" + parentMenuId);
            for (int i = 0; i < dr.Length; i++)
            {
                //添加一行数据
                DataRow row = newData.NewRow();
                row["id"] = int.Parse(dr[i]["id"].ToString());
                row["modulename"] = dr[i]["modulename"].ToString();
                row["title"] = dr[i]["title"].ToString();
                row["navigateurl"] = dr[i]["navigateurl"].ToString();
                row["imageurl"] = dr[i]["imageurl"].ToString();
                row["sortindex"] = int.Parse(dr[i]["sortindex"].ToString());
                row["parentmenuid"] = int.Parse(dr[i]["parentmenuid"].ToString());
                row["remark"] = dr[i]["remark"].ToString();
                newData.Rows.Add(row);
                //调用自身迭代
                this.GetChilds(oldData, newData, int.Parse(dr[i]["id"].ToString()));
            }
            
            
        }
        #endregion

        #region 扩展方法

        public int GetMenuId(string title)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id from X_Menu");
            strSql.Append(" where title=@title");
            SqlParameter[] parameters ={
                                          new SqlParameter("@title",SqlDbType.VarChar,200)};
            parameters[0].Value = title;
            string str = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString()));
            return Utils.StrToInt(str, 0);
        }
        #endregion
    }
}
